﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using BCNPCObject.DB;
using DbLinq;
using DbLinq.Oracle;
using CMISLibrary.Entities;
using CMISLibrary;

namespace BCNPCObject.THONGTIN_KHANG
{
    public class cls_HDG_KHACH_HANG_Controller
    {
        #region   Atributes

        //private DataSet CMIS_Header = new CMISOutputParameter();
        private HDG_KHACH_HANG info;

        #endregion

        #region   Constructor

        public cls_HDG_KHACH_HANG_Controller()
        {
            info = new HDG_KHACH_HANG();
        }
        public cls_HDG_KHACH_HANG_Controller(HDG_KHACH_HANG Info)
        {
            this.info = Info;
        }

        #endregion

        #region   Properties

        public HDG_KHACH_HANG pInfor
        {
            get { return info; }
            set { info = value; }
        }

        #endregion

        #region Methods
        public bool insert_HDG_KHACH_HANG(ref string maloi)
        {
            //long _ID = CMIS2.DB.ExecuteCommand("Select SEQ_HDG_HOP_DONG.NEXTVAL from dual");
            //if (_ID == -1) return false;
            try
            {
                //info.HDG_KHACH_HANG = Convert.ToInt64(_ID);
                HDG_KHACH_HANG objHDG_KHACH_HANG = new HDG_KHACH_HANG()
                {
                    MA_DVIQLY = info.MA_DVIQLY,
                    MA_KHANG = info.MA_KHANG,
                    SO_NHA = info.SO_NHA,
                    DUONG_PHO = info.DUONG_PHO,
                    DCHI_HDON = info.DCHI_HDON,
                    ID_HDONG = info.ID_HDONG,
                    MASO_THUE = info.MASO_THUE,
                    TLE_THUE = info.TLE_THUE,
                    LOAI_KHANG = info.LOAI_KHANG,
                    MANHOM_KHANG = info.MANHOM_KHANG,
                    MA_NN = info.MA_NN,
                    TEN_KHANG = info.TEN_KHANG,
                    TENHDON_KHANG = info.TENHDON_KHANG,
                    TENTAT_KHANG = info.TENTAT_KHANG,
                    NGAY_SINH = info.NGAY_SINH,
                    NGAY_HLUC = info.NGAY_HLUC,
                    TRANG_THAI = info.TRANG_THAI,
                    THANG = info.THANG,
                    NAM = info.NAM,
                    MA_DVIDCHINH = info.MA_DVIDCHINH,
                    TKHOAN_KHANG = info.TKHOAN_KHANG,
                    MA_NHANG = info.MA_NHANG,
                    MA_KHTT = info.MA_KHTT,
                    MA_LOAIDN = info.MA_LOAIDN,
                    DTHOAI = info.DTHOAI,
                    FAX = info.FAX,
                    EMAIL = info.EMAIL,
                    WEBSITE = info.WEBSITE,
                    GIOI_TINH = info.GIOI_TINH,
                    //HTHUC_NHAN_HDON = info.HTHUC_NHAN_HDON,
                    MA_CNANG = info.MA_CNANG,
                    NGAY_TAO = info.NGAY_TAO,
                    NGUOI_TAO = info.NGUOI_TAO,
                    NGAY_SUA = info.NGAY_SUA,
                    NGUOI_SUA = info.NGUOI_SUA
                };
                CMIS2.DB.HDG_KHACH_HANG.InsertOnSubmit(info);
                CMIS2.DB.SubmitChanges();
                //id_HDG_HOP_DONG = _ID;
                CMIS2.ResetDB();
                return true;
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB();
                return false;
            }
        }

        public bool update_HDG_KHACH_HANG(ref string strErr)
        {
            try
            {
                HDG_KHACH_HANG objHDG_KHACH_HANG = CMIS2.DB.HDG_KHACH_HANG.Single(c => c.MA_DVIQLY == info.MA_DVIQLY && c.MA_KHANG == info.MA_KHANG && c.NGAY_HLUC == info.NGAY_HLUC && c.TRANG_THAI == info.TRANG_THAI);

                objHDG_KHACH_HANG.DTHOAI = info.DTHOAI;
                objHDG_KHACH_HANG.EMAIL = info.EMAIL;
                objHDG_KHACH_HANG.NGAY_SINH = info.NGAY_SINH;
                objHDG_KHACH_HANG.NGAY_SUA = info.NGAY_SUA;
                objHDG_KHACH_HANG.NGUOI_SUA = info.NGUOI_SUA;

                CMIS2.DB.SubmitChanges();

                return true;

            }            
            catch(Exception ex)
            {
                CMIS2.ResetDB();
                strErr = ex.Message;
                return false;
            }
        }

        public bool delete_HDG_KHACH_HANG(DataTable dt, ref string maloi)
        {
            try
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        string strMadviqly = dt.Rows[i]["MA_DVIQLY"].ToString();
                        string strMakhang = dt.Rows[i]["MA_KHANG"].ToString();
                        DateTime dtNgayHluc = Convert.ToDateTime(dt.Rows[i]["NGAY_HLUC"]);
                        var query = from p in CMIS2.DB.HDG_KHACH_HANG
                                    where p.MA_DVIQLY == strMadviqly
                                    && p.MA_KHANG == strMakhang
                                    && p.NGAY_HLUC == dtNgayHluc
                                    && p.TRANG_THAI == 1
                                    select p;
                        if (query != null && query.Count() > 0)
                        {
                            CMIS2.DB.HDG_KHACH_HANG.DeleteAllOnSubmit(query.ToList());
                            CMIS2.DB.SubmitChanges();
                        }
                    }
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                maloi = ex.ToString();
                CMIS2.ResetDB(); return false;
            }
        }
        #endregion
    }
}
